package cn.tedu.project.oa.repair.service.impl;

import cn.tedu.project.oa.asset.pojo.dto.AssetSaveParam;
import cn.tedu.project.oa.asset.pojo.entity.Asset;
import cn.tedu.project.oa.asset.service.AssetService;
import cn.tedu.project.oa.repair.mapper.RepairMapper;
import cn.tedu.project.oa.repair.pojo.dto.RepairFinish;
import cn.tedu.project.oa.repair.pojo.dto.RepairQuery;
import cn.tedu.project.oa.repair.pojo.dto.RepairSaveParam;
import cn.tedu.project.oa.repair.pojo.entity.Repair;
import cn.tedu.project.oa.repair.pojo.vo.RepairVO;
import cn.tedu.project.oa.repair.service.RepairService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class RepairServiceImpl implements RepairService {

    @Autowired
    RepairMapper repairMapper;
    @Autowired
    AssetService assetService;

    @Override
    public void saveRepair(RepairSaveParam repairSaveParam) {
        log.debug("申请维修单:repairSaveParam={}",repairSaveParam);

        Repair repair = new Repair();
        BeanUtils.copyProperties(repairSaveParam,repair);
        if(repair.getId() != null){
            repairMapper.update(repair);
        }else {
            repair.setStatus(10);
            repair.setAssetStatus(3);
            repair.setApplicationTime(new Date());
            repair.setAssetUpdateTime(repair.getApplicationTime());
            repairMapper.insert(repair);

            AssetSaveParam assetSaveParam = new AssetSaveParam();
            assetSaveParam.setStatus(3);
            assetSaveParam.setUpdateTime(repair.getAssetUpdateTime());
            assetSaveParam.setCode(repairSaveParam.getAssetCode());
            assetService.updateAssetByAssetCode(assetSaveParam);
        }

    }

    @Override
    public List<RepairVO> selectList(RepairQuery repairQuery) {
        log.debug("查询维修列表业务:repairQuery={}",repairQuery);

        List<RepairVO> list = repairMapper.selectList(repairQuery);

        return list;
    }

    @Override
    public RepairVO selectById(Long id) {
        log.debug("查看维修单详情业务:id={}",id);

        RepairVO repairVO = repairMapper.selectById(id);

        return repairVO;
    }

    @Override
    public void updateFinish(RepairFinish repairFinish) {
        log.debug("完成维修处理业务:repairFinish={}",repairFinish);

        Repair repair = new Repair();
        BeanUtils.copyProperties(repairFinish,repair);
        repair.setAssetUpdateTime(new Date());
        repairMapper.update(repair);

        AssetSaveParam assetSaveParam = new AssetSaveParam();
        assetSaveParam.setCode(repairFinish.getAssetCode());
        assetSaveParam.setStatus(1);
        assetSaveParam.setUpdateTime(repair.getAssetUpdateTime());
        assetService.updateAssetByAssetCode(assetSaveParam);



    }

    @Override
    public void deleteRepair(Long id) {
        log.debug("删除维修单业务:id={}",id);

        repairMapper.deleteById(id);

    }

    @Override
    public void updateStatusById(RepairSaveParam repairSaveParam) {
        log.debug("更新审批状态业务: repairSaveParam={}",repairSaveParam);

        Repair repair = new Repair();
        BeanUtils.copyProperties(repairSaveParam,repair);
        repair.setAssetUpdateTime(new Date());
        repairMapper.update(repair);

    }
}
